<?php

namespace app\model\user;

use app\base\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 模型-UserMemberModel
 * Class UserMemberModel
 * @package app\model
 */
class UserMemberModel extends BaseModel
{
    use SoftDelete;

    protected $name = 'user_member';

    /**
     * 获取分页列表
     * @param $current_page
     * @param $list_rows
     * @param string $field
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getPageData($current_page, $list_rows, $field = 'a.*,b.nick_name,b.head_img,b.phone')
    {
        $dataList = $this->alias('a')
            ->join("t_user b", "a.user_id=b.id")
            ->field($field)
            ->order('create_time', 'desc')
            ->paginate(['page' => $current_page, 'list_rows' => $list_rows], false);
        $current = $dataList->currentPage();
        $total = $dataList->total();
        $limit = $dataList->listRows();
        $ret = ['total' => $total, 'current' => $current, 'limit' => $limit, 'dataList' => $dataList->items()];
        return $ret;
    }

    /**
     * @param $id
     * @param $status  状态 1审核中 2审核失败 3审核通过
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function changeMember($id, $status)
    {

        $ret = self::where(['id' => $id])->update(['status' => $status, 'check_time' => date("Y-m-d H:i:s")]);

        if ($ret !== false) {
            if (intval($status) == 3) {
                $user_member = self::where(['id' => $id])->field('popularize_code,user_id')->find();
                $user_model = new UserModel();
                $user_data = $user_model->where('popularize_code', $user_member['popularize_code'])->field('id')->find();
                $up_user['top_user_id'] = $user_data['id'];
                $up_user['top_time'] = date("Y-m-d H:i:s");
                $up_user['member_level'] = 1;
                $up_user['member_date'] = 4746230369;
                $user_model->where("id", $user_member['user_id'])->update($up_user);
            }
            return true;
        }
        return false;
    }

    /**
     * 添加会员申请
     * @param $user_id
     * @param $popularize_code 邀请码
     * @param $group_name  所在群名称
     * @param $photo  群截图照片
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function addMember($user_id, $popularize_code, $group_name, $photo)
    {
        $data = self::where(['user_id' => $user_id])->find();
        if (empty($data)) {
            $in_data = [
                'user_id' => $user_id,
                'popularize_code' => $popularize_code,
                'group_name' => $group_name,
                'photo' => $photo,
                'update_time' => time(),
                'create_time' => time(),
            ];
            $ret = self::insert($in_data);
        } else {
            $up_data = [
                'user_id' => $user_id,
                'popularize_code' => $popularize_code,
                'group_name' => $group_name,
                'photo' => $photo,
                'update_time' => time()
            ];
            $id = $data['id'];
            $ret = self::where(['id' => $id])->update($up_data);
        }
        if ($ret === false) {
            return false;
        }
        return true;
    }
}